<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
require_once("content_negotiation/include.php");

/**
 * RDF
 * 
 * Show the RDF statements for the object specified.
 */
class escape_repositoryui_action_Rdf extends escape_repositoryui_Action
{
	public function handleRequest()
	{
		/* @var $repository escape_repository_Repository */
		$repository =& $this->requestAttributes['repository'];
		
		// fetch the ID from the request
		$objectId = $_REQUEST['actionValue'];
		$objectUri = $repository->convertDataObjectIdToObjectUri($objectId);
		
		// get the resource
		try {
			$oreObject = $repository->getOreObjectByUri($objectUri);
		}
		catch(Exception $exception)
		{
			// couldn't find one of the resources
			header("HTTP/1.1 404 Not Found");
			header("Status: 404 Not Found");
			header('Content-Type: text/plain');
			print "Object not found!\n";
			
			return null;
		}
		
		// fetch the aggregation URL
		$aggregation = $oreObject->getAggregation();
		$aggregationUrl = $aggregation->getExternalUri();
		
		// fetch the resource map URL
		$remUrl = $aggregation->getRemExternalUri();
		
		// add link to aggregation to headers
		header('Link: <' . $aggregationUrl . '>; rel="aggregation"', true);
		// add link to resource map to headers
		header('Link: <' . $remUrl . '>; type="application/rdf+xml"; rel="resourcemap"', false);
		
		// fetch the RDF representation of the aggregated resource
		//TODO: make this more efficient
		$remRdfDocument = $aggregation->generateResourceMapDocument();
		$oreObjectRdfResource = $remRdfDocument->getResourceBySubject($oreObject->getExternalUri());
		
		// send the appropriate HTTP headers
		header('Content-Type: application/rdf+xml; charset=UTF-8');
		header('Content-Disposition: inline; filename="' . $objectId . '.rdf"');
		
		//TODO: content negotiation for other types of RDF
		
		// send the RDF/XML to the output
		print $oreObjectRdfResource->getRdfXml();
		
		return null;
	}
}